<template>
  <span>{{ label }}</span>
</template>

<script setup>
import { getCommonData } from "@/api/project/common.js";
const dataList = ref([]);
const props = defineProps({
  dataList: {
    type: Array,
    default: () => [],
  },
  value: "", //数据真实的值
  valueKey: {
    type: String,
    default: "code",
  },
  labelKey: {
    type: String,
    default: "name",
  },
  isFetch: {
    type: Boolean,
    default: false,
  },
  paramsName: String,
});
const label = computed(() => {
  const list = props.isFetch ? dataList.value : props.dataList;
  if (!list.length || !props.value) {
    return "";
  }
  let current = list.filter((item) => {
    return item[props.valueKey] == props.value;
  });
  if (current.length) {
    return current[0][props.labelKey];
  } else {
    return "";
  }
});
//获取数据
const getDataList = async () => {
  const params = {
    bizName: props.paramsName,
  };
  try {
    const res = await getCommonData(params);
    dataList.value = res.data;
  } catch (error) {
    console.log(error);
  }
};
onMounted(() => {
  props.isFetch && getDataList();
});
</script>

<style scoped lang="scss"></style>
